package com.gcd.skymallorderapi.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.model.Cart;
import com.situ.model.Order;
import com.situ.model.search.OrderSearchBean;
import com.situ.service.OrderService;
import com.situ.utils.JsonResult;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping(value = "/api/v1/order", produces = MediaType.APPLICATION_JSON_VALUE)
public class OrderApi {
    private OrderService orderService;

    @Autowired
    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }

    //查询订单
    @GetMapping
    ResponseEntity<JsonResult<?>> findAll(@RequestParam(defaultValue = "1") Integer pageNo,
                                          @RequestParam(defaultValue = "10") Integer pageSize,
                                          OrderSearchBean searchBean) {
        System.out.println("searchBean: " + searchBean);
        Page<Order> page = new Page<>(pageNo, pageSize);
        page = this.orderService.findAll(page, searchBean);
        return ResponseEntity.ok(JsonResult.success(page));
    }

    //根据秒杀编号查询订单信息
    @GetMapping("/seckill/{seckillNo}")
    ResponseEntity<JsonResult<?>> findBySeckillNo(@PathVariable String seckillNo) {
        Order order = orderService.findBySeckillNo(seckillNo);
        if (order == null) {
            return ResponseEntity.ok(JsonResult.fail("未查询到该秒杀订单信息"));
        } else {
            return ResponseEntity.ok(JsonResult.success(order));
        }
    }

    //根据id查询订单
    @GetMapping("/id/{id}")
    ResponseEntity<JsonResult<?>> findById(@PathVariable Integer id) {
        Order order = orderService.findById(id);
        if (order == null) {
            return ResponseEntity.ok(JsonResult.fail("未查询到该订单信息"));
        } else {
            return ResponseEntity.ok(JsonResult.success(order));
        }
    }


    //创建普通订单
    @PostMapping
    ResponseEntity<JsonResult<?>> createOrder(@RequestBody List<Cart> carts) {
        boolean result = this.orderService.createOrder(carts);
        if (result) {
            return ResponseEntity.ok(JsonResult.success());
        } else {
            return ResponseEntity.ok(JsonResult.fail("订单创建失败"));
        }
    }


    //创建秒杀订单
    @PostMapping("/seckill_order")
    ResponseEntity<JsonResult<?>> createSeckillOrder(@RequestBody Order order) {
        // 确保在这里能正确接收到userId
        if (order.getUserId() == null) {
            // 处理userId为空的情况
            return ResponseEntity.badRequest().body(JsonResult.fail("用户ID不能为空"));
        }
        Order success = orderService.createSeckillOrder(order);

        if (!success.getTotalAmount().equals(BigDecimal.ZERO)) {
            return ResponseEntity.ok(JsonResult.success(order));
        } else {
            return ResponseEntity.ok(JsonResult.fail("创建订单失败"));
        }
    }


    //前台点击支付按钮，将支付状态和订单状态改为已支付（1）
    @PutMapping("/payStatusTo1/{id}")
    public ResponseEntity<JsonResult<?>> updatePayStatusTo1(@PathVariable Integer id) {
        boolean result = this.orderService.updatePayStatus(id);
        if (result) {
            return ResponseEntity.ok(JsonResult.success());
        } else {
            return ResponseEntity.ok(JsonResult.fail("修改支付状态失败"));
        }
    }

    //后台点击发货，将发货状态改为已发货（2）
    @PutMapping("/statusTo2/{id}")
    public ResponseEntity<JsonResult<?>> updateOrderStatusTo2(@PathVariable Integer id) {
        boolean result = this.orderService.updateOrderStatusTo2(id);
        if (result) {
            return ResponseEntity.ok(JsonResult.success());
        } else {
            return ResponseEntity.ok(JsonResult.fail("修改发货状态失败"));
        }
    }

    //点击确认收货，将收货状态改为已收货（3）
    @PutMapping("/statusTo3/{id}")
    public ResponseEntity<JsonResult<?>> updateOrderStatusTo3(@PathVariable Integer id) {
        boolean result = this.orderService.updateOrderStatusTo3(id);
        if (result) {
            return ResponseEntity.ok(JsonResult.success());
        } else {
            return ResponseEntity.ok(JsonResult.fail("修改收货状态失败"));
        }
    }

    //12小时后未支付，取消订单(4)
    @PutMapping("/statusTo4/{id}")
    public ResponseEntity<JsonResult<?>> updateOrderStatusTo4(@PathVariable Integer id) {
        boolean result = this.orderService.updateStatusTo4(id);
        if (result) {
            return ResponseEntity.ok(JsonResult.success("true"));
        } else {
            return ResponseEntity.ok(JsonResult.fail("修改订单状态失败"));
        }
    }

    //前台用户点击退款，将订单状态改为退款待确认（5）
    @PutMapping("/statusTo5/{id}")
    public ResponseEntity<JsonResult<?>> updateOrderStatusTo5(@PathVariable Integer id) {
        boolean result = this.orderService.updateStatusTo5(id);
        if (result) {
            return ResponseEntity.ok(JsonResult.success());
        } else {
            return ResponseEntity.ok(JsonResult.fail("修改订单状态失败"));
        }
    }

    //退款成功，将订单状态改为退款成功（6）
    @PutMapping("/statusTo6/{id}")
    public ResponseEntity<JsonResult<?>> updateOrderStatusTo6(@PathVariable Integer id) {
        boolean result = this.orderService.updateStatusTo6(id);
        if (result) {
            return ResponseEntity.ok(JsonResult.success());
        } else {
            return ResponseEntity.ok(JsonResult.fail("修改订单状态失败"));
        }
    }

}
